package com.site.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

/**
 * author xiaochi
 * date  2021/2/17
 */
@Slf4j
@Component
@SuppressWarnings("all")
public class AsyncUtil {

    private static Executor asyncExecutor;

    @Autowired
    public void setAsyncExecutor(Executor asyncExecutor) {
        AsyncUtil.asyncExecutor = asyncExecutor;
    }

    public static CompletableFuture<Void> run(Runnable runnable) {
        return CompletableFuture.runAsync(runnable,asyncExecutor).exceptionally(e -> {
            log.error("异步执行异常[{}]",e);
            return null;
        });
    }

    public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs){
        return CompletableFuture.allOf(cfs).exceptionally(e -> {
            log.error("异步allOf执行异常[{}]",e);
            return null;
        });
    }
}
